package servlet;

import bean.User;
import bean.attendance;
import dao.impl.UserDaoImpl;
import dao.impl.attendanceDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/attendanceServlet")
public class attendanceServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //防止中文乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String method=req.getParameter("method");

        if(method.equals("attFind")){

            List<attendance> list=new attendanceDaoImpl().attFind();
            int at=new attendanceDaoImpl().attendanceNum();
            int Noat=new attendanceDaoImpl().NoattendanceNum();
            req.setAttribute("NoNum",Noat);
            req.setAttribute("Num",at);
            req.setAttribute("list",list);
            req.getRequestDispatcher("/attendance.jsp").forward(req,resp);
            //req.getRequestDispatcher("/attendance.jsp").forward(req,resp);
        }else if (method.equals("rname_searchFind")){
            String rname=req.getParameter("rname");
            if(rname.equals("Search")){
                resp.sendRedirect("attendanceServlet?method=attFind");
            }else{
                List<attendance> list=new attendanceDaoImpl().rname_searchFind(rname);
                req.setAttribute("list",list);
                req.getRequestDispatcher("/attendance.jsp").forward(req,resp);
            }
        }else if (method.equals("datetime_searchFind")){
            String datetime=req.getParameter("datetime");
            if (datetime.equals("Search")){
                resp.sendRedirect("attendanceServlet?method=attFind");
            }else {
                List<attendance> list=new attendanceDaoImpl().datetime_searchFind(datetime);
                req.setAttribute("list",list);
                req.getRequestDispatcher("/attendance.jsp").forward(req,resp);
            }
        }else if (method.equals("usernaem_uid_datetime")){
            String datetime=req.getParameter("datetime");
            String rname=req.getParameter("rname");
            User u=new UserDaoImpl().FindUid(rname);
            int uid=u.getUid();
            List<attendance> list=new attendanceDaoImpl().rnaem_uid_Findday(uid,datetime);

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
